// src/services/deptServices.js
import request from './request';

// 处理请求返回的数据
const handleResponse = (response) => {
  const { code, msg = '' } = response;
  if (code !== 0) {
    throw new Error(msg);
  }
  return response;
};

// 获取所有部门
export const fetchDepartments = async () => {
  const response = await request('/department');
  return handleResponse(response);
};

// 通过id查询部门信息
export const getDepartment = async (deptId) => {
  const response = await request(`/sysDepartment/${deptId}`, 'GET');
  return handleResponse(response);
};

// 添加部门
export const addDepartment = async (deptData) => {
  const response = await request('/sysDepartment', 'POST', deptData);
  return handleResponse(response);
};

// 修改部门信息
export const editDepartment = async (deptData) => {
  const response = await request('/sysDepartment', 'PUT', deptData);
  return handleResponse(response);
};

// 删除部门
export const deleteDepartment = async (deptId) => {
  const response = await request(`/sysDepartment/${deptId}`, 'DELETE');
  return handleResponse(response);
};
